/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.skatiger.jrove.spring.form.component;

import java.io.IOException;

import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.component.JroveAttr;
import org.skatiger.jrove.renderer.Html;
import org.skatiger.jrove.spring.tags.NestedPath;
import org.springframework.beans.PropertyAccessor;
import org.springframework.core.Conventions;

public class Form extends HtmlFormComponent {

    /**
     * The default HTTP method using which form values are sent to the server:
     * "post"
     */
    public static final String DEFAULT_METHOD = "post";
    /** The default attribute name: &quot;command&quot; */
    public static final String DEFAULT_COMMAND_NAME = "command";

    /** The name of the '<code>modelAttribute</code>' setting */
    public static final String MODEL_ATTRIBUTE = "modelAttribute";
    /**
     * The name of the {@link javax.servlet.jsp.PageContext} attribute under
     * which the form object name is exposed.
     */
    public static final String MODEL_ATTRIBUTE_VARIABLE_NAME = Conventions
            .getQualifiedAttributeName(Form.class, MODEL_ATTRIBUTE);

    public static final String DEFAULT_METHOD_PARAM = "_method";

    private String modelAttribute = DEFAULT_COMMAND_NAME;

    private String action;

    private String name;

    private String method;

    private String target;

    private String enctype;

    private String acceptCharset;

    private String methodParam = DEFAULT_METHOD_PARAM;

    // previousNestedPath
    private String previousNestedPath;

    public String getModelAttribute() {
        return getPropertyValue("modelAttribute", modelAttribute);
    }

    public void setModelAttribute(String modelAttribute) {
        this.modelAttribute = modelAttribute;
    }

    public void setCommandName(String commandName) {
        setModelAttribute(commandName);
    }

    public String getCommandName(String commandName) {
        return getModelAttribute();
    }

    public String getAction() {
        return getPropertyValue("action", action);
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getMethod() {
        String _method = getDeclareMethod();
        return isMethodBrowserSupported(_method) ? _method : DEFAULT_METHOD;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getTarget() {
        return getPropertyValue("target", target);
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getEnctype() {
        return getPropertyValue("enctype", enctype);
    }

    public void setEnctype(String enctype) {
        this.enctype = enctype;
    }

    public String getAcceptCharset() {
        return getPropertyValue("acceptCharset", acceptCharset);
    }

    public void setAcceptCharset(String acceptCharset) {
        this.acceptCharset = acceptCharset;
    }

    public String getMethodParam() {
        return getPropertyValue("methodParam", methodParam);
    }

    public void setMethodParam(String methodParam) {
        this.methodParam = methodParam;
    }

    public String getName() {
        return getPropertyValue("name", name);
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * Determine if the HTTP method is supported by browsers (i.e. GET or POST).
     */
    private boolean isMethodBrowserSupported(String _method) {
        return (Html.FORM_METHOD_GET.equalsIgnoreCase(_method) || Html.FORM_METHOD_POST
                .equalsIgnoreCase(_method));
    }

    public String getDeclareMethod() {
        String _method = getPropertyValue("method", method);
        return _method;
    }

    /**
     * Determine if the HTTP method is supported by browsers (i.e. GET or POST).
     */
    public boolean isMethodBrowserSupported() {
        return isMethodBrowserSupported(getDeclareMethod());
    }

    @Override
    public void encodeBegin(ViewContext context) throws IOException {
        prepareModelAttribute();
        super.encodeBegin(context);
    }

    protected void prepareModelAttribute() {
        ViewContext context = getViewContext();
        context.setAttribute(Form.MODEL_ATTRIBUTE_VARIABLE_NAME, modelAttribute);
        this.previousNestedPath = (String) context
                .getAttribute(NestedPath.NESTED_PATH_VARIABLE_NAME);
        context.setAttribute(NestedPath.NESTED_PATH_VARIABLE_NAME,
                modelAttribute + PropertyAccessor.NESTED_PROPERTY_SEPARATOR);

    }

    @Override
    public void encodeEnd(ViewContext context) throws IOException {
        super.encodeEnd(context);
        releaseModelAttribute();
    }

    protected void releaseModelAttribute() {
        ViewContext context = getViewContext();
        context.removeAttribute(MODEL_ATTRIBUTE_VARIABLE_NAME);
        if (this.previousNestedPath != null) {
            // Expose previous nestedPath value.
            context.setAttribute(NestedPath.NESTED_PATH_VARIABLE_NAME,
                    this.previousNestedPath);
        } else {
            // Remove exposed nestedPath value.
            context.removeAttribute(NestedPath.NESTED_PATH_VARIABLE_NAME);
        }

    }

    @Override
    public String getStyleClass() {
        return getPropertyValue(JroveAttr.STYLE_CLASS_ATTR, null);
    }

}
